package org.desktop.web.client.project.form;

import org.desktop.web.client.custom.widget.AbstractWindow;
import org.desktop.web.client.project.grid.SubProjectsGrid;
import org.desktop.web.client.project.service.ProjectService;
import org.desktop.web.client.project.service.ProjectServiceAsync;
import org.desktop.web.client.util.JavascriptUtil;
import org.desktop.web.shared.domain.entities.Project;
import org.desktop.web.shared.domain.entities.SubProject;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.IsWidget;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.box.AlertMessageBox;
import com.sencha.gxt.widget.core.client.box.MessageBox;
import com.sencha.gxt.widget.core.client.button.ButtonBar;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutData;
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutPack;
import com.sencha.gxt.widget.core.client.container.MarginData;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.form.DateField;
import com.sencha.gxt.widget.core.client.form.FieldLabel;
import com.sencha.gxt.widget.core.client.form.TextField;

public class SubProjectFormWindow extends AbstractWindow implements IsWidget {

	private SubProjectsGrid toUpdate;

	private Project project;;

	private FramedPanel mainPanel;

	private TextField subProjectName;

	private DateField startDate;

	private DateField endDate;

	public SubProjectFormWindow(Project project, SubProjectsGrid toUpdate) {
		super();
		this.project = project;
		this.toUpdate = toUpdate;
		setModal(true);
		mainPanel = new FramedPanel();
		mainPanel.setHeaderVisible(false);
		setHeaderVisible(true);
		setHeadingText("Add to project " + project.getDescProject());

	}

	private void addSaveEvent(TextButton save, final Project project,
			final SubProjectsGrid grid) {
		save.addSelectHandler(new SelectHandler() {

			@Override
			public void onSelect(SelectEvent event) {
				SubProject sub = new SubProject();
				if (!JavascriptUtil.isUndefined(getEndDate().getValue())) {
					sub.setEndDate(getEndDate().getValue());
				}
				sub.setDescSubProject(getSubProjectName().getValue());
				sub.setStartDate(getStartDate().getValue());
				ProjectServiceAsync service = GWT.create(ProjectService.class);
				service.saveSubProject(Cookies.getCookie("token"),
						project.getId(), sub, new AsyncCallback<Boolean>() {

							@Override
							public void onFailure(Throwable caught) {

								AlertMessageBox amb = new AlertMessageBox(
										"Error", caught.getMessage());
								amb.setIcon(MessageBox.ICONS.error());
								amb.show();
							}

							@Override
							public void onSuccess(Boolean result) {
								// TODO Auto-generated method stub
								if (result) {
									grid.loadByRowSelected(project);
									hide();

								}

							}
						});
			}
		});
	}

	private void addCancelEvent(TextButton cancel) {
		cancel.addSelectHandler(new SelectHandler() {

			@Override
			public void onSelect(SelectEvent event) {
				hide();

			}
		});
	}

	@Override
	public String id() {
		// TODO Auto-generated method stub
		return "SUB_PROJECT_FORM";
	}

	public TextField getSubProjectName() {
		return subProjectName;
	}

	public DateField getStartDate() {
		return startDate;
	}

	public DateField getEndDate() {
		return endDate;
	}

	@Override
	public void init() {
		VerticalLayoutContainer fieldsContainer = new VerticalLayoutContainer();
		TextField projectName = new TextField();
		projectName.setEnabled(false);
		projectName.setValue(project.getDescProject());
		fieldsContainer.add(new FieldLabel(projectName, "Nome progetto"),
				new VerticalLayoutData(1, -1));

		subProjectName = new TextField();
		subProjectName.setAllowBlank(false);
		fieldsContainer.add(new FieldLabel(subProjectName, "Nome"),
				new VerticalLayoutData(1, -1));

		startDate = new DateField();
		startDate.setAllowBlank(false);
		fieldsContainer.add(new FieldLabel(startDate, "Data inizio"),
				new VerticalLayoutData(1, -1));

		endDate = new DateField();
		endDate.setAllowBlank(true);
		fieldsContainer.add(new FieldLabel(endDate, "Data fine"),
				new VerticalLayoutData(1, -1));

		ButtonBar buttonBar = new ButtonBar();
		TextButton save = new TextButton("Save");
		TextButton cancel = new TextButton("Cancel");
		buttonBar.add(save, new BoxLayoutData(new Margins(10)));
		buttonBar.add(cancel, new BoxLayoutData(new Margins(10)));

		setButtonAlign(BoxLayoutPack.CENTER);
		addButton(buttonBar);

		addCancelEvent(cancel);
		addSaveEvent(save, project, toUpdate);

		mainPanel.add(fieldsContainer, new MarginData(10));

		add(mainPanel);
	}

}
